﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class RoleModuleService
    {
        /// <summary>
        /// 根据角色id获取模块id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<int> GetModuleId(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.RoleModule.Where(p => p.RoleId == id).Select(p => p.ModuleId).ToList();
            }
        }

        /// <summary>
        /// 删除角色模块
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int GetDel(string ids, int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string sql = $"delete RoleModule where ModuleId in ({ids}) and RoleId = {id}";
                return db.Database.ExecuteSqlCommand(sql);
            }
        }
        /// <summary>
        /// 添加角色模块
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int GetAdd(string ids, int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string[] idArr = ids.Split(',');
                foreach (var item in idArr)
                {
                    RoleModule userRole = new RoleModule() { RoleId = id, ModuleId = Convert.ToInt32(item) };
                    db.RoleModule.Add(userRole);
                }
                return db.SaveChanges();
            }
            
        }




    }
}
